package testforschool;

import java.util.Stack;

public class Caculator {


    public static boolean isnumber(char x){
        if(x >= '0' && x <='9'){
            return true;
        } else
            return false;
    }

    static int priority(char x){
        if(x=='+'||x=='-'){
            return 0;
        }else if (x=='*'||x=='/'){
            return 1;
        }else if(x=='#')
            return -2;
        return -3;
    }

    public static int caclulate(String s){
        Stack<Integer> number = new Stack<>();
        Stack<Character> operate = new Stack<>();
        char top;
        int a;
        int b;
//        boolean flag = false;
        for(int i=0;i<s.length();i++){
            if (isnumber(s.charAt(i))){
                int Temp=0;
                String temp="";
                temp+=s.charAt(i);
                //连续多个数字字符，判断多位数
                while(isnumber(s.charAt(++i))){
                    temp+=s.charAt(i);
                }
                for(int jj = 0;jj<temp.length();++jj){
                    Temp=Temp*10+temp.charAt(jj)-48;
                    //ASC码转换
                }
                number.push(Temp);
                temp=null;
            }
            if(!isnumber(s.charAt(i))){
                if(operate.empty()){
                    //操作符压栈
                    operate.push(s.charAt(i));
                }else{
                    //返回栈顶元素
                    top = operate.peek();
                    System.out.println(top);
                    //运算符优先级比较
                    if(priority(s.charAt(i))>priority(top)){
                        //优先级高的压栈
                        operate.push(s.charAt(i));
                    }else{
                        //当下一个符号的优先级小于上一个时会跳到这里 执行了上一个优先级高的运算符的运算
                        while (priority(s.charAt(i))<=priority(top)){
                            if (top=='#'&&s.charAt(i)=='#'){
                                //用于输出结果
                                int answer;
                                operate.pop();
                                answer=number.peek();
                                number.pop();
                                return answer;
                            }else {
                                //栈顶元素
                                a=number.peek();
                                number.pop();//弹出
                                b=number.peek();
                                number.pop();
                                System.out.println("a="+a+"\nb="+b);
                            }
                            if(top=='+'){
                                b+=a;          //运算后压栈
                                number.push(b);
                            }else if(top=='-'){
                                b-=a;
                                number.push(b);
                            }else if(top=='*'){
                                b*=a;
                                number.push(b);
                            }else if(top=='/'){
                                b/=a;
                                number.push(b);
                            }
                            operate.pop();//运算结束后运算符弹栈
                            top=operate.peek(); //top返回栈顶元素
                        }
                        operate.push(s.charAt(i));
                        //因为之前的比较，所以新来的这个运算符没参与运算，所以压栈
                    }
                }
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        String s = "#21*3+5-10+3*9#";
        System.out.println("输入：\n"+s);
        int answer = caclulate(s);
        System.out.println("结果:"+answer);
    }

}
